//
//  NSData+WJCategory.m
//  横向选择器
//
//  Created by apple on 15/10/10.
//  Copyright (c) 2015年 tqh. All rights reserved.
//

#import "NSDate+WJCategory.h"
#import "NSDateFormatter+WJCategory.h"
@implementation NSDate (WJCategory)

- (NSString *)timeIntervalDescription
{
 
    NSTimeInterval timeInterval =- [self timeIntervalSinceNow];
    if (timeInterval < 60) {
        return @"1分钟内";
    } else if (timeInterval < 3600) {
        return [NSString stringWithFormat:@"%.f分钟前", timeInterval / 60];
    } else if (timeInterval < 86400) {
        return [NSString stringWithFormat:@"%.f小时前", timeInterval / 3600];
    } else if (timeInterval < 2592000) {//30天内
        return [NSString stringWithFormat:@"%.f天前", timeInterval / 86400];
    } else if (timeInterval < 31536000) {//30天至1年内
        NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormat:@"M月d日"];
        return [dateFormatter stringFromDate:self];
    } else {
        return [NSString stringWithFormat:@"%.f年前", timeInterval / 31536000];
    }
}


- (NSString *)minuteDescription
{
    NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormat:@"yyyy-MM-dd"];
    
    NSString *theDay = [dateFormatter stringFromDate:self];//日期的年月日
    NSString *currentDay = [dateFormatter stringFromDate:[NSDate date]];//当前年月日
    if ([theDay isEqualToString:currentDay]) {//当天
        [dateFormatter setDateFormat:@"ah:mm"];
        return [dateFormatter stringFromDate:self];
    } else if ([[dateFormatter dateFromString:currentDay] timeIntervalSinceDate:[dateFormatter dateFromString:theDay]] == 86400) {//昨天
        [dateFormatter setDateFormat:@"ah:mm"];
        return [NSString stringWithFormat:@"昨天 %@", [dateFormatter stringFromDate:self]];
    } else if ([[dateFormatter dateFromString:currentDay] timeIntervalSinceDate:[dateFormatter dateFromString:theDay]] < 886400 * 7) {//间隔一周内
        [dateFormatter setDateFormat:@"EEEE ah:mm"];
        return [dateFormatter stringFromDate:self];
    } else {//以前
        [dateFormatter setDateFormat:@"yyyy-MM-dd ah:mm"];
        return [dateFormatter stringFromDate:self];
    }
}


- (NSString *)formattedDateDescription
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *theDay = [dateFormatter stringFromDate:self];//日期的年月日
    NSString *currentDay = [dateFormatter stringFromDate:[NSDate date]];//当前年月日
    
    NSInteger timeInterval = -[self timeIntervalSinceNow];
    if (timeInterval < 60) {
        return @"1分钟内";
    } else if (timeInterval < 3600) {//1小时内
        return [NSString stringWithFormat:@"%ld分钟前", timeInterval / 60];
    } else if (timeInterval < 21600) {//6小时内
        return [NSString stringWithFormat:@"%ld小时前", timeInterval / 3600];
    } else if ([theDay isEqualToString:currentDay]) {//当天
        [dateFormatter setDateFormat:@"HH:mm"];
        return [NSString stringWithFormat:@"今天 %@", [dateFormatter stringFromDate:self]];
    } else if ([[dateFormatter dateFromString:currentDay] timeIntervalSinceDate:[dateFormatter dateFromString:theDay]] == 86400) {//昨天
        [dateFormatter setDateFormat:@"HH:mm"];
        return [NSString stringWithFormat:@"昨天 %@", [dateFormatter stringFromDate:self]];
    } else {//以前
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        return [dateFormatter stringFromDate:self];
    }
}

+ (NSString *)timeCountdown:(NSTimeInterval)timer {    
    NSInteger date = timer - [[NSDate date] timeIntervalSince1970];//时间差
  if (date > 86400){
       return [NSString stringWithFormat:@"%ld天%ld时",date/86400,(date%84600)/3600];
    }else if (date > 3600){
       return [NSString stringWithFormat:@"%ld小时,%ld分",date/3600,(date%3600)/60];
    }else if (date > 60){
        return [NSString stringWithFormat:@"%ld分%ld秒",date / 60,(date%60)];
    }else if (date > 0){
        return [NSString stringWithFormat:@"%ld秒",date];
    }else{
        return [NSString stringWithFormat:@"现在"];
    }
}

+ (NSString *)timeWithNSTimeInterval:(NSTimeInterval)timer {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timer];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)timeWithHHmmNSTimeInterval:(NSTimeInterval)timer {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timer];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)timeWithNSTimeIntervalISYear:(NSTimeInterval)timer {
    //2014-10-31
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timer];
    return [dateFormatter stringFromDate:date];
}



+ (CGFloat)timeFloatWithNSTimeInterval:(NSTimeInterval)timer creatTimer:(NSTimeInterval)creatTimer {
    //创建时间
    //现在时间
    //未来时间 - 创建时间
    //未来 － 现在
    NSInteger cha = timer - creatTimer;
    NSInteger cha1 = [[NSDate date] timeIntervalSince1970]- creatTimer;
    CGFloat f = (CGFloat)cha1/(CGFloat)cha;
    return f;
//    NSInteger date = timer - [[NSDate date] timeIntervalSince1970];//时间差
//    NSInteger date1 = timer- creatTimer;
//    return (CGFloat)(date1-date)/date1;
}
#pragma mark - 获取某个日期所在年份的天数
+ (NSInteger)getNumberOfDaysOneYear:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    
//    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
//    
//    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
//    
//    NSInteger iCurYear = [components year];  //当前的年份
//    
//    NSInteger iCurMonth = [components month];  //当前的月份
//    
//    NSInteger iCurDay = [components day];  // 当前的号数
    return range.length;
}

@end
